package cn.edu.baiyunu.myapplication.data.repository

import cn.edu.baiyunu.myapplication.R
import cn.edu.baiyunu.myapplication.data.db.dao.PhotoRecordDao
import cn.edu.baiyunu.myapplication.data.db.entity.PhotoRecord
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flow

class PhotoRepositoryImpl(
    private val photoRecordDao: PhotoRecordDao
) : PhotoRepository {
    
    // 测试数据
    private val testRecords = listOf(
        PhotoRecord(
            id = 1,
            filePath = "/storage/emulated/0/Pictures/photo_id_1.jpg",
            thumbnailPath = R.drawable.ic_photo_placeholder.toString(),
            specType = "一寸照",
            backgroundColor = "蓝色",
            createdAt = System.currentTimeMillis() - 86400000, // 昨天
            width = 25,
            height = 35,
            isAiOptimized = true
        ),
        PhotoRecord(
            id = 2,
            filePath = "/storage/emulated/0/Pictures/photo_id_2.jpg",
            thumbnailPath = R.drawable.ic_photo_placeholder.toString(),
            specType = "二寸照",
            backgroundColor = "白色",
            createdAt = System.currentTimeMillis() - 172800000, // 前天
            width = 35,
            height = 45,
            isAiOptimized = true
        ),
        PhotoRecord(
            id = 3,
            filePath = "/storage/emulated/0/Pictures/photo_id_3.jpg",
            thumbnailPath = R.drawable.ic_photo_placeholder.toString(),
            specType = "护照照",
            backgroundColor = "红色",
            createdAt = System.currentTimeMillis() - 259200000, // 三天前
            width = 33,
            height = 48,
            isAiOptimized = false
        )
    )
    
    override suspend fun savePhotoRecord(photoRecord: PhotoRecord): Long {
        return photoRecordDao.insert(photoRecord)
    }
    
    override fun getAllPhotoRecords(): Flow<List<PhotoRecord>> {
        return photoRecordDao.getAllPhotoRecords()
    }
    
    override fun getPhotoRecordById(id: Long): Flow<PhotoRecord?> {
        return photoRecordDao.getPhotoRecordById(id)
    }
    
    override fun getPhotoRecordsBySpecType(specType: String): Flow<List<PhotoRecord>> {
        return photoRecordDao.getPhotoRecordsBySpecType(specType)
    }
    
    override suspend fun deletePhotoRecord(id: Long) {
        photoRecordDao.deleteById(id)
    }
    
    override suspend fun deleteAllPhotoRecords() {
        photoRecordDao.deleteAll()
    }
    
    override fun searchPhotoRecords(query: String): Flow<List<PhotoRecord>> {
        return photoRecordDao.searchPhotoRecords(query)
    }
    
    override fun getRecentPhotoRecords(limit: Int): Flow<List<PhotoRecord>> {
        return photoRecordDao.getRecentPhotoRecords(limit)
    }
    
    override suspend fun getPhotoById(id: Long): PhotoRecord? {
        return photoRecordDao.getPhotoById(id)
    }
} 